package com.cnswhy.cloud.server.merchant.controller.order;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.cnswhy.cloud.common.core.base.R;
import com.cnswhy.cloud.common.core.base.controller.SuperController;
import com.cnswhy.cloud.common.core.base.request.PageParams;
import com.cnswhy.cloud.common.core.utils.DateUtils;
import com.cnswhy.cloud.common.rabbitmq.service.MessageService;
import com.cnswhy.cloud.server.common.entity.constant.AmqpExchange;
import com.cnswhy.cloud.server.common.entity.mq.SpellDayendCancelMsg;
import com.cnswhy.cloud.server.core.appservice.AppSpellOrderService;
import com.cnswhy.cloud.server.core.dto.BuyBackOrderPageDTO;
import com.cnswhy.cloud.server.core.dto.BuyBackOrderSaveDTO;
import com.cnswhy.cloud.server.core.dto.BuyBackOrderUpdateDTO;
import com.cnswhy.cloud.server.core.dto.SpellOrderDetailPageDTO;
import com.cnswhy.cloud.server.core.entity.BuyBackOrder;
import com.cnswhy.cloud.server.core.entity.ShopOrder;
import com.cnswhy.cloud.server.core.entity.SpellOrderDetail;
import com.cnswhy.cloud.server.core.service.customer.CustomerReceiverService;
import com.cnswhy.cloud.server.core.service.spell.BuyBackOrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

//import com.github.zuihou.security.annotation.PreAuth;


/**
 * <p>
 * 前端控制器
 * 
 * </p>
 *
 * @author cnswhy
 * @date 2020-10-17
 */
@Slf4j
@Validated
@RestController
@RequestMapping("/buyBackOrder")
@Api(value = "BuyBackOrder", tags = "")
//@PreAuth(replace = "buyBackOrder:")
public class BuyBackOrderController extends SuperController<BuyBackOrderService, Long, BuyBackOrder, BuyBackOrderPageDTO, BuyBackOrderSaveDTO, BuyBackOrderUpdateDTO> {
    @Autowired
    CustomerReceiverService customerReceiverService;
    @Override
    public void handlerResult(IPage<BuyBackOrder> page) {
        for(BuyBackOrder buyBackOrder : page.getRecords()){
            buyBackOrder.setCustomerReceiver(customerReceiverService.getById(buyBackOrder.getAddressId()));
        }
    }

    @Autowired
    MessageService messageService;
    @PostMapping("/buyback")
    public R<Boolean> buyback(String settDate){
        messageService.sendQueueByObject(AmqpExchange.BUY_BACK_ORDER_MESSAGE,SpellDayendCancelMsg.builder()
                .settDate(settDate).toString());
        return success();
    }
    /*@Autowired
    AppSpellOrderService appSpellOrderService;
    @PostMapping("/notifySpellOrder")
    public R<Boolean> notifySpellOrder(String tradeNo,String outTradeNo){
        appSpellOrderService.notifyPay(outTradeNo, tradeNo);

        return success();
    }*/
    /**
     * Excel导入后的操作
     *
     * @param list
     */
    @Override
    public R<Boolean> handlerImport(List<Map<String, String>> list){
        List<BuyBackOrder> buyBackOrderList = list.stream().map((map) -> {
            BuyBackOrder buyBackOrder = BuyBackOrder.builder().build();
            return buyBackOrder;
        }).collect(Collectors.toList());

        return R.success(baseService.saveBatch(buyBackOrderList));
    }
}
